package com.lz.ui_lib.widget.gif;

import android.content.Context;
import android.graphics.PixelFormat;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

/**
 * 基类SurfaceView
 * <p>
 * Created by Liuyk on 2015/10/27.
 */
public abstract class BaseSurfaceLayout extends SurfaceView implements SurfaceHolder.Callback {

    public SurfaceHolder mSurfaceHolder;

    public BaseSurfaceLayout(Context context) {
        this(context, null);
    }

    public BaseSurfaceLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BaseSurfaceLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mSurfaceHolder = getHolder();//SurfaceHolder是SurfaceView的控制接口
        mSurfaceHolder.setKeepScreenOn(true);
        mSurfaceHolder.setFormat(PixelFormat.TRANSLUCENT);
        mSurfaceHolder.addCallback(this);
//        mSurfaceHolder.setFixedSize(mWidth, mHeight);//显示的分辨率,不设置为视频默认
        setFocusable(true);
        setFocusableInTouchMode(true);
        setZOrderOnTop(true);
    }


    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        //在创建时激发，可以在这里调用画图的线程。
        baseSurfaceCreated(holder);
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        //在surface的大小发生改变时激发
        baseSurfaceChanged(holder, format, width, height);
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        //销毁时激发，可以在这里将画图的线程停止、释放。
        baseSurfaceDestroyed(holder);
    }

    public abstract void baseSurfaceCreated(SurfaceHolder holder);

    public abstract void baseSurfaceDestroyed(SurfaceHolder holder);

    public abstract void baseSurfaceChanged(SurfaceHolder holder, int format, int width, int height);

}
